package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.BalanceDetailsDao;
import com.baichang.taoshunda.dao.OrderDao;
import com.baichang.taoshunda.entity.BalanceDetails;
import com.baichang.taoshunda.entity.Order;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.service.BalanceDetailsService;
import com.baichang.taoshunda.service.OrderService;
import com.baichang.taoshunda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by ${wushenglu} on 17/09/27.
 */
@Service
public class BalanceDetailsServiceImpl implements BalanceDetailsService {
    @Autowired
    private BalanceDetailsDao balanceDetailsDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private UserService userService;

    /**
     * 用户账单明细
     *
     * @param orderNumber
     * @return
     */
    @Override
    public boolean saveBalanceDetail(String orderNumber) throws ServiceException {
        Order order = orderDao.getOrderByOrderNumber(orderNumber);
        if (order == null) {
            throw new ServiceException("订单不存在");
        }
        User user = userService.getUserBeanById(order.getBuyerId());
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        int souceType = 1;
        String state = "-";
        BigDecimal money = BigDecimal.valueOf(0);
        return saveDetail(order, souceType, state, money);

    }

//    @Override
//    public boolean saveReturnMoney(Order order, Integer state) {
//        return saveDetail(order, state, "+", BigDecimal.valueOf(0));
//    }

    public boolean saveDetail(Order order, int souceType, String state, BigDecimal money) {
        BalanceDetails balanceDetails = new BalanceDetails();
        balanceDetails.setBalance(order.getAllMoney());
        balanceDetails.setSourceType(souceType);
        balanceDetails.setCreated(new Date());
        balanceDetails.setUserId(order.getBuyerId());
        balanceDetails.setBalanceState(state);
        balanceDetails.setOrderNumber(order.getOrderNumber());
        balanceDetails.setCurrentAmount(money);
        return balanceDetailsDao.save(balanceDetails) > 0;
    }


    @Override
    public boolean saveUserBalance(Integer userId, Double allMoney, Integer state,String orderNumber) {
        BalanceDetails balanceDetails = new BalanceDetails();
        balanceDetails.setBalance(BigDecimal.valueOf(allMoney));
        String moneyState = "+";
        if (allMoney < 0) {
            moneyState = "-";
        }
        balanceDetails.setSourceType(state);
        balanceDetails.setCreated(new Date());
        balanceDetails.setUserId(userId);
        balanceDetails.setBalanceState(moneyState);
        balanceDetails.setOrderNumber(orderNumber);
        balanceDetails.setCurrentAmount(BigDecimal.valueOf(0));
        return balanceDetailsDao.save(balanceDetails) > 0;
    }
}
